package com.jingming.consult.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jingming.consult.R;
import com.jingming.consult.base.BaseActivity;
import com.hjq.toast.ToastUtils;

import org.jetbrains.annotations.Nullable;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetUserInfoCallback;
import cn.jpush.im.android.api.model.UserInfo;

public class SearchUserActivity extends BaseActivity {
    private TextView tvSearch, tvUser;
    private EditText etSearch;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, SearchUserActivity.class);
        if (!(context instanceof Activity)) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchuser);
        initView();
    }

    private void initView() {
        tvSearch = findViewById(R.id.tvSearch);
        etSearch = findViewById(R.id.etSearch);
        tvUser = findViewById(R.id.tvUser);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(etSearch.getText().toString().trim())) {
                    JMessageClient.getUserInfo(
                            etSearch.getText().toString().trim(),
                            new GetUserInfoCallback() {
                                @Override
                                public void gotResult(int responseCode, String responseMessage, UserInfo userInfo) {
                                    if (responseCode == 0) {
                                        ToastUtils.show(responseMessage);
                                        tvUser.setText((TextUtils.isEmpty(userInfo.getNickname())) ? userInfo.getNickname() :
                                                userInfo.getNickname());

                                        tvUser.setVisibility(View.VISIBLE);
                                    } else {
                                        ToastUtils.show("该用户不存在");
                                    }
                                }
                            });
                } else {
                    ToastUtils.show("请输入用户名");
                }
            }
        });
    }
}
